<?php
/**
 * Created by PhpStorm.
 * User: 65736
 * Date: 2019/4/3 0003
 * Time: 14:02
 */

namespace app\common\model\wechat;


use app\common\model\CommonModel;

class WeChatUserModel extends CommonModel
{
    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'db_config_wechat';

    /**
     * 表名称
     *
     * @var string
     */
    protected $table = "wechat_user";


    /**
     * 初始化方法
     * {@inheritDoc}
     * @see CommonModel::initialize()
     */
    protected function initialize()
    {
        parent::initialize();

    }


    /**
     * 删除关注用户数据
     * @param array $user_wx_data 用户微信数据
     * @return boolean
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function deleteUserData(array $user_wx_data)
    {
        if (!$user_wx_data) {
            $this->set_error(__FUNCTION__, "删除关注用户数据失败[用户微信数据为空]");
            return false;
        }
        $open_id = $user_wx_data['openid'];
        // 判断用户是否存在于微信公众号用户
        $map = array(
            'open_id' => $open_id,
            //'union_id' => $union_id,
        );
        $user_wechat_data = $this->where($map)->find();
        if ($user_wechat_data) {
            $res = $this->where($map)->delete();
            if (!$res) {
                $this->set_error(__FUNCTION__, "删除关注用户数据失败[删除失败]");
                return false;
            }
            $wechatUserCoinModel = WeChatUserCoinModel::getInstance();
            $map = array(
                'wechat_user_id' => $user_wechat_data['id'],
            );
            $wechatUserCoinModel->where($map)->delete();

            //取消交易所 18.11.14 @thinkphper
            $exmodel = WeChatExchangeModel::getInstance();
            $exmodel->where($map)->delete();
        }
        return true;
    }

    /**
     * 初始化关注用户数据
     * @param array $user_wx_data 用户微信数据
     * @return boolean
     */
    public function initUserData(array $user_wx_data)
    {
        if (!$user_wx_data) {
            $this->set_error(__FUNCTION__, "微信公众号初始化用户数据失败[用户微信数据为空]");
            return false;
        }
        $open_id = $user_wx_data['openid'];
        $union_id = isset($user_wx_data['unionid']) ? $user_wx_data['unionid'] : '';

        $avatar = $user_wx_data['headimgurl'];
        $city = $user_wx_data['city'];
        $country = $user_wx_data['country'];
        $nickname = $user_wx_data['nickname'];
        $province = $user_wx_data['province'];
        $sex = $user_wx_data['sex'];

        // 判断用户是否存在于微信公众号用户
        $map = array(
            'open_id' => $open_id,
            'union_id' => $union_id,
        );
        $user_wechat_data = $this->where($map)->find();
        if (!$user_wechat_data) {
            // 不存在,插入
            $insert_wechat_data = [
                'open_id' => $open_id,
                'union_id' => $union_id,
                'avatar' => $avatar,
                'nickname' => $nickname,
                'sex' => $sex,
                'country' => $country,
                'province' => $province,
                'city' => $city,
            ];
            $add_res = $this->add_data($insert_wechat_data);
            if (!$add_res) {
                $this->set_error(__FUNCTION__, "微信公众号初始化用户数据失败[插入失败]");
                return false;
            }
            return $add_res;
        }
        return $user_wechat_data['id'];
    }

    /**
     * 获取open_id
     * @return [type] [description]
     */
    public function getWechatUids()
    {
        $open_id_list = $this->column("open_id");
        return $open_id_list;
    }
}